import socket
import sys
from game import packer, Card, Command, PORT, trim_msg


if len(sys.argv) > 1 :
	HOST = sys.argv[1]
else :
	HOST = "localhost"

# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


class Player : 
	def __init__(self, uid) :
		self.uid = uid
		self.bet = 0
		self.cards = []
		self.name = "noname"		

my_uid = ""
players = dict()

def print_all_players_info() :
	print ""
	print "##############################################"
	for cur_user in players :
		print_card(players[cur_user])
	print "##############################################"
	print ""

def print_card(player) :
	msg = player.name + ":"
	for cur_card in xrange(len(player.cards)) :
		if (not my_uid == player.uid) and cur_card == 0:
			msg = msg + "A Hidden Card | "
		else :
			card = player.cards[cur_card]
			msg = msg + str(card) + " | "
	print msg

try:
	sock.connect((HOST, PORT))
	# Connect to server and send data
	
	# bet request
	while 1 :
		reply = sock.recv(packer.size)
		if reply == "" :
			continue
		reply = packer.unpack(reply)
					
		if reply[0] == Command.CMD_BET_QUERY : 
			bet = int(input("Input your bet:"))
			pOut = packer.pack(Command.CMD_BET_REPLY,"",bet,0)
			sock.sendall(pOut)
		
		elif reply[0] == Command.CMD_UID :
			my_uid = trim_msg(reply[1])
			player = Player(my_uid)
			print my_uid + " added"
			players[my_uid] = player
			# after recv uid, query for username
			while 1 :
				name = raw_input("Input your username and password separated with a space:")
				pOut = packer.pack(Command.CMD_NAMING,name,0,0)
				sock.sendall(pOut)
				reply = sock.recv(packer.size)
				if reply == "" :
					continue
				reply = packer.unpack(reply)
				print reply[1]
				if reply[2] == 0 :
					break;

			players[my_uid].name = name
			
			cmd = 0
			cmd = raw_input("Press Enter when you are ready...")
			pOut = packer.pack(Command.CMD_READY,"",0,0)
			sock.sendall(pOut)				
			
		# player enter the game room	
		elif reply[0] == Command.CMD_PLAYER_ENTER :
			uid = trim_msg(reply[1])
			if not uid == my_uid :
				player = Player(uid)
				players[uid] = player
				print "User " + str(uid) + " entered."
			
		# rename
		# event format is "uid name"
		elif reply[0] == Command.CMD_NAMING : 
			arg = reply[1]
			pos = str(arg).find(" ")
			uid = trim_msg(str(arg)[:pos])
			name = str(arg)[pos + 1:]
			players[uid].name = trim_msg(name)
			print "User " + str(uid) + " is now known as " + name		
			
		#message
		elif reply[0] == Command.CMD_MSG :
			print ">>>>>" + reply[1]	
			
		# other player got card
		elif reply[0] == Command.CMD_MSG_GETCARD :
			uid = trim_msg(reply[1])
			if not uid == my_uid :	
				card = Card(int(reply[2]), int(reply[3]))
				players[uid].cards.append(card)
				print_all_players_info()
			
		# pass card by dealer event
		elif reply[0] == Command.CMD_GIVE_CARD :	
			card = Card(int(reply[2]), int(reply[3]))
			players[my_uid].cards.append(card)
			print "Got card " + str(card) + "."	
			print_all_players_info()
			
		# user renaming event	
		elif reply[0] == Command.CMD_ADD_QUERY :	
			more = input(reply[1])
			if more == 1 :
				pOut = packer.pack(Command.CMD_ADD_REPLY,"",1,0)
				sock.sendall(pOut)
			else :
				pOut = packer.pack(Command.CMD_ADD_REPLY,"",0,0)
				sock.sendall(pOut)
		
		# game end event
		elif reply[0] == Command.CMD_RESULT :	
			print reply[1] + " for " + str(reply[2]) + "."
			break			
finally:
	sock.close()

